
// //初始化当前页//
var page = 1;

//一页的条数
var pagesize = 10;
//ajax
function ajax(url, data,successFunc,type, dataType) {
	var type = type || 'post';//请求类型
	var dataType = dataType || 'json';//接收数据类型
	var success = success || function (res) {
		 var res = res;
		 // console.log(res);
		 layer.closeAll('loading');
		 // setTimeout(function () {
			 layer.msg(res.msg)
		 // },1000)
		 // layer.msg(res.msg)
			if(res.code == 200){
				successFunc(res)
				$("img").on("error", function() {
	                $(this).attr("src", "../../image/error.png");
	            });
			}else if (res.code == 400) {
			  	layer.msg('登陆信息错误，请重新登陆');
			  	window.location.href = "login.html";
		  	}else if(res.code == 555){
				layer.msg('请求失败,没有权限访问');
				window.location.href = "login.html";
			}
		};
	var error = error || function (res) {
			/*res.status;//错误状态吗*/
			layer.closeAll('loading');
			setTimeout(function () {
				if(res.code == 404){
					layer.msg('请求失败，请求未找到');
				}else if (res.code == 400) {
			  		layer.msg('登陆信息错误，请重新登陆');
			  		window.location.href = "login.html";
				}else if(res.code == 555){
					layer.msg('请求失败,没有权限访问');
			  		window.location.href = "login.html";
				}else {
					layer.msg('请求失败,网络连接超时');
				}
			},500);
		};
	$.ajax({
		'url': url,
		'data': data,
		'type': type,
		'dataType': dataType,
		'success': success,
		'error': error,
		'beforeSend': function () {
			layer.load(2);
		},
		xhrFields: {
			withCredentials: true
		}
	});
}

//分页
pagelist = function (res){
	OpendList(res);
}
pageNumList = function (res){
	
	OpendList(res);
  	$('#pageNum strong').html('0')
  	$('#totalNum strong').html('0')
  	$('#totalNum strong').html(res.data.count)
  	if (res.data.count >= 1) {
	  	var pageCount = Math.ceil(res.data.count/pagesize); //取到pageCount的值
	  	$('#pageNum strong').html(pageCount)
	  	var currentPage = page; //得到currentPage
 	}else if(res.data.count == 0){
	  	$('#pageNum strong').html('0')
	  	$("#example").empty();
	  	return false;
    }

     var options = {
  	   bootstrapMajorVersion: 3, //版本
  	   currentPage: currentPage, //当前页数
  	   totalPages: pageCount, //总页数
  	   itemTexts: function (type, page, current) {
  		   switch (type) {
  			   	case "first":
  				   return "首页";
  			   	case "prev":
  				   return "上一页";
  			   	case "next":
  				   return "下一页";
  			   	case "last":
  				   return "末页";
  			   	case "page":
  				   return page;
  		   }
  	   },//点击事件，用于通过Ajax来刷新整个List列表
  	   onPageClicked: function (event, originalEvent, type, page) {
		   data.page = page;
		   ajax(
		   		pageUrls,
				data,
				pagelist,
				'post',
				'json'
	   		)
			console.log(data);
  	   }
   };
    $('#example').bootstrapPaginator(options);
}
